// /////////////////////////////////////////////////////////////////////////////
//
// Name:            CommunicatorScript.cpp
// Author:          Michael Bartsch
//
// Desc:            This file contains the Glue functions that enable the
//                  interaction between CCommunicator and Lua.
//
// License:         Copyright (C) 2008-2009 Michael Bartsch
//
//                  This program is free software: you can redistribute it
//                  and/or modify it under the terms of the GNU General Public
//                  License as published by the Free Software Foundation,
//                  either version 3 of the License, or (at your option) any
//                  later version.
//
//                  This program is distributed in the hope that it will be
//                  useful, but WITHOUT ANY WARRANTY; without even the implied
//                  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
//                  PURPOSE. See the GNU General Public License for more
//                  details.
//
//                  You should have received a copy of the GNU General Public
//                  License along with this program. If not, see
//                  <http://www.gnu.org/licenses/>.
//
// /////////////////////////////////////////////////////////////////////////////

// Include required header file(s).
#include "CommunicatorScript.h"


// Registers CCommunicator.
void registerCommunicator(lua_State *L)
{
    // Register low level functions.
    lua_register(L, "comSerialWrite",        lua_com_serialWrite);
    lua_register(L, "comSerialConnect",      lua_com_serialConnect);
    lua_register(L, "comGetSelectedDevice",  lua_com_getSelectedDevice);

    // Register the CCommunicator class.
    char buffer[] =
    {
       "CCommunicator = {}\n" \
       "CCommunicator.__index = CCommunicator\n" \
       "\n" \
       "function CCommunicator.create()\n" \
       "   local handle = {}\n" \
       "   setmetatable(handle, CCommunicator)\n" \
       "   return handle\n" \
       "end\n" \
       "\n" \
       "function CCommunicator:serialWrite(value) comSerialWrite(value) end\n" \
       "function CCommunicator:serialConnect(value) comSerialConnect(value) end\n" \
       "\n" \
       "function CCommunicator:getSelectedDevice() return comGetSelectedDevice(value) end\n" \
       "\n" \
       "Communicator = CCommunicator.create()"
    };

    luaL_dostring(L, buffer);
}

// Glue functions:
// Glue function for CCommunicator::serialWrite
int lua_com_serialWrite(lua_State *L)
{
   const char *lua_var;
   int lua_argnum = lua_gettop(L);

   if(lua_argnum >= 1)   lua_var = lua_tostring(L, 1);

   CCommunicator::Instance()->serialWrite(lua_var);

   return 0;
}

// Glue function for CCommunicator::serialReconnect
int lua_com_serialConnect(lua_State *L)
{
   int lua_var;
   int lua_argnum = lua_gettop(L);

   if(lua_argnum >= 1)   lua_var = lua_tonumber(L, 1);

   CCommunicator::Instance()->serialConnect(lua_var);
   return 0;
}

// Glue function for CCommunicator::getSelectedDevice
int lua_com_getSelectedDevice(lua_State *L)
{
   lua_pushnumber(L, CCommunicator::Instance()->getSelectedDevice());
   return 1;
}
// End of File.
